<?php

/**
 * blog actions.
 *
 * @package    Your name
 * @subpackage blog
 * @author     John Doe <you@example.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class blogActions extends FrontendActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->pager = $this->getPostsPager($request);
  }

  public function executeBuildRssAllPosts(sfWebRequest $request)
  {
    $query = Doctrine_Core::getTable('BlogPost')->getPublishedPostsQuery();
    $this->pager = $this->getPostsPager($request, $query);
  }

  public function executeUnsuscribe(sfWebRequest $request)
  {
    $this->registration = $this->getRoute()->getObject();
    $this->registration->delete();
  }

  public function executeByCategory(sfWebRequest $request)
  {
    $this->category = $this->getRoute()->getObject();
    
    $query = Doctrine_Core::getTable('BlogPost')->getPublishedPostsByCategory(
      $this->getUser()->getCulture(),
      $this->category->getSlug()
    );
    
    $this->pager = $this->getPostsPager($request, $query);
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->post = $this->getRoute()->getObject();

    $this->getUser()->setReadPosts(explode('|', $request->getCookie('posts')));

    if (!$this->getUser()->hasRead($this->post->getId()))
    {
      $this->post->incrementHits();
      $this->getUser()->read($this->post->getId());
      $this->getResponse()->setCookie('posts', implode('|', $this->getUser()->getReadPosts()), strtotime('+30 days'));
    }
  }

  public function executeAddComment(sfWebRequest $request)
  {
    $this->post = $this->getRoute()->getObject();
    
    $comment = new BlogComment();
    $comment->setPost($this->post);
    $comment->setIsPublished(!$this->post->isModerated());

    $this->form = new BlogCommentForm($comment, array(
      'env' => sfConfig::get('sf_environment'),
      'culture' => $this->getUser()->getCulture()
    ));

    $this->form->bind($request->getParameter($this->form->getName()));
    
    if ($this->form->isValid())
    {
      $comment = $this->form->save();

      $this->sendNotificationToAdministrator($comment);

      if (!$this->post->isModerated())
      {
        $this->sendNotifications($comment);
      }

      return $this->redirect($this->generateUrl('post_show', $this->post));
    }
    
    $this->setTemplate('show');
  }
  
  protected function sendNotificationToAdministrator(BlogComment $comment)
  {
    $this->getMailer()->composeAndSend(
      sfConfig::get('app_contact_email'),
      sfConfig::get('app_contact_email'),
      "[HugoHamon.com] Notification d'un nouveau commentaire",
      $this->getPartial(
        'notification/fr/newAdminComment', 
        array('comment' => $comment)
      )
    );
  }

  protected function sendNotifications(BlogComment $comment)
  {
    foreach ($comment->getPost()->getSubscribersExcept($comment->getEmail()) as $subscriber)
    {
      $this->getMailer()->composeAndSend(
        sfConfig::get('app_contact_email'),
        $subscriber->getEmail(),
        $this->context->getI18N()->__(
          '[HugoHamon.com] New comment for "%post%"', 
          array('%post%' => $comment->getPost()->getTitle()),
          'notifications'
        ),
        $this->getPartial(
          'notification/'. $subscriber->getCulture() .'/newComment', 
          array('suscriber' => $subscriber, 'comment' => $comment)
        )
      );
    }
  }
}
